import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
} from '@nestjs/common';
import { PostService } from './post.service';
import { Request } from 'express';
import { Post as PostEntity } from '../../entities/post.entity';
import { User } from '../../entities/user.entity';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiParam,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import {
  PagePostItemtDto,
  PostRegisterDto,
} from '../../dto/post/post-register.dto';
import { Public } from '../../common/decorators/public.decorator';
import { PaginatedResponseDto } from '../../common/class/res.class';
import { Singer } from '../../entities/singer.entity';

@ApiTags('宣传内容')
@ApiBearerAuth()
@Controller('post')
export class PostController {
  constructor(private readonly postService: PostService) {}

  @ApiOperation({ summary: '创建文章' })
  @ApiBody({ type: PostRegisterDto, description: '输入创建的文章信息' })
  @Post('save')
  async createPost(
    @Req() req: Request,
    @Body() createInput: PostRegisterDto | any,
  ): Promise<any> {
    createInput.user = req.user;

    await this.postService.create(createInput);
    return '创建成功';
  }

  @ApiOperation({ summary: '删除文章' })
  @Delete('delete')
  async remove(@Body() obj: any): Promise<any> {
    await this.postService.remove(obj.id);
    return '删除文章成功';
  }

  @ApiOperation({ summary: '更新文章' })
  @ApiBody({ type: PostRegisterDto, description: '输入更新文章' })
  @Post('edit')
  async update(@Body() updateInpt: PostEntity): Promise<any> {
    await this.postService.update(updateInpt);
    return '更新成功';
  }
  @Get('page')
  @ApiOperation({ summary: '分页获取歌手信息' }) // 操作的摘要为 分页获取歌手信息
  @ApiQuery({
    name: 'pageSize',
    description: '每页数量',
    type: 'number',
    required: false,
  }) // 查询参数描述为 每页数量
  @ApiQuery({
    name: 'pageNo',
    description: '当前页码',
    type: 'number',
    required: false,
  }) // 查询参数描述为 当前页码
  @ApiOkResponse({ description: '返回分页结果' }) // 成功响应的描述信息
  async page(
    @Query() query: PagePostItemtDto,
  ): Promise<PaginatedResponseDto<Singer>> {
    const [records, total] = await this.postService.page(query);
    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: query.pageSize,
        pageNo: query.pageNo,
        total,
      },
    };
  }
  @ApiOperation({ summary: '根据用户的信息查询所有文章' })
  @Get()
  async findAll(@Req() req: Request): Promise<any> {
    const data = await this.postService.findAll((req.user as User).id);
    return data; // 查询该用户的所有文章
  }

  @ApiOperation({ summary: '查询单个帖子' })
  @ApiQuery({ name: 'pollId', required: true, description: '投票的ID' })
  @Get('findOneById')
  async findOne(@Query('pollId') pollId: string): Promise<any> {
    console.log(pollId, 'pollId');

    const data = await this.postService.findAll(pollId);
    return data; // 查询单个文章
  }
}
